﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace E1_eMart
{
    public partial class AccountInfo : System.Web.UI.Page
    {
        protected TaiKhoan taiKhoan = new TaiKhoan();
        protected ThongTinTaiKhoan thongTinTaiKhoan = new ThongTinTaiKhoan();     

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateTime ngaySinh = new DateTime();
                DataClasses1DataContext edata = new DataClasses1DataContext();
                if (Request.Params["mtk"] == null)
                    return;
                int mtk = int.Parse(Request.Params["mtk"].ToString());
                if (mtk <= 0)
                    return;
                taiKhoan = edata.TaiKhoans.Single<TaiKhoan>(tk => tk.MaTaiKhoan == mtk);
                var itemQuery2 = from tttk in edata.ThongTinTaiKhoans
                                 where tttk.MaTaiKhoan == taiKhoan.MaTaiKhoan
                                 select tttk;
                thongTinTaiKhoan = itemQuery2.ToList()[0];
                if (thongTinTaiKhoan == null)
                    return;

                tbName.Text = thongTinTaiKhoan.HoTen;
                if (thongTinTaiKhoan.GioiTinh.CompareTo("Nữ") == 0)
                    drdlGioiTinh.SelectedIndex = 0;
                else
                    drdlGioiTinh.SelectedIndex = 1;
                ngaySinh = thongTinTaiKhoan.NgaySinh.Value;
                int thang = ngaySinh.Month;
                int ngay = ngaySinh.Day;
                int nam = ngaySinh.Year;
                drdlThang.SelectedIndex = thang;
                drdlNgay.SelectedIndex = ngay;
                drdlNam.SelectedIndex = (nam - 1994) * (-1);
                tbEmail.Text = thongTinTaiKhoan.Email;
                tbRepeatEmail.Text = thongTinTaiKhoan.Email;
                tbAddress.Text = thongTinTaiKhoan.DiaChi;
                tbPhone.Text = thongTinTaiKhoan.SoDienThoai;
                tbCardIndenNumber.Text = thongTinTaiKhoan.MaTheTinhDung;
                tbUsername.Text = taiKhoan.TenTaiKhoan;
                Image1.ImageUrl = "~/images/avatar/" + taiKhoan.Avatar;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext edata = new DataClasses1DataContext();
            if (Request.Params["mtk"] == null)
                return;
            int mtk = int.Parse(Request.Params["mtk"].ToString());
            if (mtk <= 0)
                return;
            taiKhoan = edata.TaiKhoans.Single<TaiKhoan>(tk => tk.MaTaiKhoan == mtk);
       
            if (taiKhoan != null)
            {
                //Update info TaiKhoan
                taiKhoan.TenTaiKhoan = tbUsername.Text.Trim();
                taiKhoan.MatKhau = tbPassword.Text;
                //Upload avatar to Server
                if (fuAvatar.HasFile)
                {
                    String fileName = fuAvatar.FileName;
                    fuAvatar.SaveAs(Server.MapPath("~/images/avatar/") + fileName);                    
                    taiKhoan.Avatar = Path.GetFileName(fileName);
                }
                else{
                    taiKhoan.Avatar = taiKhoan.Avatar;
                }
                //Submit
                //edata.SubmitChanges();

                //Update info Thongtintaikhoan
                var itemQuery3 = from tttk in edata.ThongTinTaiKhoans
                                 where tttk.MaTaiKhoan == taiKhoan.MaTaiKhoan
                                 select tttk;
                thongTinTaiKhoan = itemQuery3.ToList()[0];
                thongTinTaiKhoan.HoTen = tbName.Text.Trim();
                if (drdlGioiTinh.SelectedIndex == 0)
                    thongTinTaiKhoan.GioiTinh = "Nữ";
                else
                    thongTinTaiKhoan.GioiTinh = "Nam";
                int ngay, thang, nam;
                ngay = drdlNgay.SelectedIndex;
                thang = drdlThang.SelectedIndex;
                if (drdlNam.SelectedIndex > 0)
                {
                    nam = 1994 - drdlNam.SelectedIndex;
                    DateTime ngaySinh = new DateTime(nam, thang, ngay);
                    thongTinTaiKhoan.NgaySinh = ngaySinh;
                }
                thongTinTaiKhoan.NoiSinh = thongTinTaiKhoan.NoiSinh;
                thongTinTaiKhoan.Email = tbEmail.Text.Trim();
                thongTinTaiKhoan.DiaChi = tbAddress.Text.Trim();
                thongTinTaiKhoan.SoDienThoai = tbPhone.Text.Trim();
                thongTinTaiKhoan.MaTheTinhDung = tbCardIndenNumber.Text.Trim();
                edata.SubmitChanges();
                String s = "AccountInfo.aspx?mtk=" + taiKhoan.MaTaiKhoan;
                Response.Redirect(s);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {            
            if (Session["username"] != null)
            {
                tbName.Text = "";
                tbEmail.Text = "";
                tbAddress.Text = "";
                tbPhone.Text = "";
                tbCardIndenNumber.Text = "";
                tbUsername.Text = "";
                tbPassword.Text = "";
                this.Page_Load(null, null);
            }
        }
    }
}